<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>angular知识点梳理第三篇-组件 | 茂茂个人博客</title>
    <meta name="generator" content="VuePress 1.7.1">
    <link rel="icon" href="https://pan.zealsay.com/blog/favicon.ico">
    <script>
          var _hmt = _hmt || [];
          (function() {
            var hm = document.createElement("script");
            hm.src = "https://hm.baidu.com/hm.js?b0aae218897fa9d8a9f76e9a77e0b3c6";
            var s = document.getElementsByTagName("script")[0]; 
            s.parentNode.insertBefore(hm, s);
          })();
        </script>
    <meta name="description" content="开心最重要呀">
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no">
    
    <link rel="preload" href="/blog/assets/css/0.styles.ed123d92.css" as="style"><link rel="preload" href="/blog/assets/js/app.e4cba77b.js" as="script"><link rel="preload" href="/blog/assets/js/5.1ad0ff24.js" as="script"><link rel="preload" href="/blog/assets/js/1.fe4eac58.js" as="script"><link rel="preload" href="/blog/assets/js/47.499ce120.js" as="script"><link rel="prefetch" href="/blog/assets/js/10.58980e71.js"><link rel="prefetch" href="/blog/assets/js/11.461ee1ab.js"><link rel="prefetch" href="/blog/assets/js/12.de9aade4.js"><link rel="prefetch" href="/blog/assets/js/13.c1f7abfc.js"><link rel="prefetch" href="/blog/assets/js/14.eeee4da5.js"><link rel="prefetch" href="/blog/assets/js/15.225a74ee.js"><link rel="prefetch" href="/blog/assets/js/16.3eef12b1.js"><link rel="prefetch" href="/blog/assets/js/17.a97bd7c7.js"><link rel="prefetch" href="/blog/assets/js/18.461a74bd.js"><link rel="prefetch" href="/blog/assets/js/19.7b390d19.js"><link rel="prefetch" href="/blog/assets/js/20.8638de7c.js"><link rel="prefetch" href="/blog/assets/js/21.8b1e5c62.js"><link rel="prefetch" href="/blog/assets/js/22.b6b97b5b.js"><link rel="prefetch" href="/blog/assets/js/23.c3e9364b.js"><link rel="prefetch" href="/blog/assets/js/24.daa0ce9e.js"><link rel="prefetch" href="/blog/assets/js/25.0c44cdb2.js"><link rel="prefetch" href="/blog/assets/js/26.ed604668.js"><link rel="prefetch" href="/blog/assets/js/27.0c4acdea.js"><link rel="prefetch" href="/blog/assets/js/28.4f896280.js"><link rel="prefetch" href="/blog/assets/js/29.50e69b67.js"><link rel="prefetch" href="/blog/assets/js/3.90606e56.js"><link rel="prefetch" href="/blog/assets/js/30.5c69a6a2.js"><link rel="prefetch" href="/blog/assets/js/31.8a40c25d.js"><link rel="prefetch" href="/blog/assets/js/32.7a1f46ed.js"><link rel="prefetch" href="/blog/assets/js/33.a48429b4.js"><link rel="prefetch" href="/blog/assets/js/34.8f224bd0.js"><link rel="prefetch" href="/blog/assets/js/35.bc552eb5.js"><link rel="prefetch" href="/blog/assets/js/36.7e0cc0d9.js"><link rel="prefetch" href="/blog/assets/js/37.2bb21bfb.js"><link rel="prefetch" href="/blog/assets/js/38.f77b014f.js"><link rel="prefetch" href="/blog/assets/js/39.8a5ba8b0.js"><link rel="prefetch" href="/blog/assets/js/4.89c80be7.js"><link rel="prefetch" href="/blog/assets/js/40.c600c684.js"><link rel="prefetch" href="/blog/assets/js/41.0dbac397.js"><link rel="prefetch" href="/blog/assets/js/42.f1035a7b.js"><link rel="prefetch" href="/blog/assets/js/43.ace95e0e.js"><link rel="prefetch" href="/blog/assets/js/44.0ff283b9.js"><link rel="prefetch" href="/blog/assets/js/45.836151e7.js"><link rel="prefetch" href="/blog/assets/js/46.033824d7.js"><link rel="prefetch" href="/blog/assets/js/48.39c2db90.js"><link rel="prefetch" href="/blog/assets/js/49.ca266dff.js"><link rel="prefetch" href="/blog/assets/js/50.c12ee57e.js"><link rel="prefetch" href="/blog/assets/js/51.4c3c8a1c.js"><link rel="prefetch" href="/blog/assets/js/52.c11bc4a4.js"><link rel="prefetch" href="/blog/assets/js/53.d4dc4ec7.js"><link rel="prefetch" href="/blog/assets/js/54.300dd272.js"><link rel="prefetch" href="/blog/assets/js/55.6e86af02.js"><link rel="prefetch" href="/blog/assets/js/56.d91a04d8.js"><link rel="prefetch" href="/blog/assets/js/6.4f77b856.js"><link rel="prefetch" href="/blog/assets/js/7.db9a864b.js"><link rel="prefetch" href="/blog/assets/js/8.bf0cca83.js"><link rel="prefetch" href="/blog/assets/js/9.84ddd18e.js">
    <link rel="stylesheet" href="/blog/assets/css/0.styles.ed123d92.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container no-sidebar" data-v-0c5314fc><div data-v-0c5314fc><div id="loader-wrapper" class="loading-wrapper" data-v-d48f4d20 data-v-0c5314fc data-v-0c5314fc><div class="loader-main" data-v-d48f4d20><div data-v-d48f4d20></div><div data-v-d48f4d20></div><div data-v-d48f4d20></div><div data-v-d48f4d20></div></div> <!----> <!----></div> <div class="password-shadow password-wrapper-out" style="display:none;" data-v-5cb9a64e data-v-0c5314fc data-v-0c5314fc><h3 class="title" style="display:none;" data-v-5cb9a64e data-v-5cb9a64e>茂茂个人博客</h3> <!----> <label id="box" class="inputBox" style="display:none;" data-v-5cb9a64e data-v-5cb9a64e><input type="password" value="" data-v-5cb9a64e> <span data-v-5cb9a64e>Konck! Knock!</span> <button data-v-5cb9a64e>OK</button></label> <div class="footer" style="display:none;" data-v-5cb9a64e data-v-5cb9a64e><span data-v-5cb9a64e><i class="iconfont reco-theme" data-v-5cb9a64e></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-5cb9a64e>vuePress-theme-reco</a></span> <span data-v-5cb9a64e><i class="iconfont reco-copyright" data-v-5cb9a64e></i> <a data-v-5cb9a64e><span data-v-5cb9a64e>茂茂</span>
            
          <span data-v-5cb9a64e>2019 - </span>
          2023
        </a></span></div></div> <div class="hide" data-v-0c5314fc><div data-v-0c5314fc><div id="smart" class="wrapper-page" style="background-image:url(/blog/articleImage/img07.jpeg);background-position-x:center;background-position-y:center;background-size:cover;background-repeat-x:no-repeat;background-repeat-y:no-repeat;" data-v-0c5314fc><header class="navbar" data-v-0c5314fc><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/blog/" class="home-link router-link-active"><img src="/blog/logo.png" alt="茂茂个人博客" class="logo"> <span class="site-name">茂茂个人博客</span></a> <div class="links"><div id="dayNightSwitch" class="generalWrapper" data-v-32f44868><a class="click" data-v-32f44868><div class="onOff daySwitch" data-v-32f44868><div class="star star1" data-v-32f44868></div> <div class="star star2" data-v-32f44868></div> <div class="star star3" data-v-32f44868></div> <div class="star star4" data-v-32f44868></div> <div class="star star5" data-v-32f44868></div> <div class="star sky" data-v-32f44868></div> <div class="sunMoon" data-v-32f44868><div class="crater crater1" data-v-32f44868></div> <div class="crater crater2" data-v-32f44868></div> <div class="crater crater3" data-v-32f44868></div> <div class="cloud part1" data-v-32f44868></div> <div class="cloud part2" data-v-32f44868></div></div></div></a></div> <div class="search-box"><i class="iconfont reco-search"></i> <input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/blog/" class="nav-link"><i class="iconfont reco-home"></i>
  主页
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      博客
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/blog/categories/工具与阅读/" class="nav-link"><i class="iconfont undefined"></i>
  工具与阅读
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/vue/" class="nav-link"><i class="iconfont undefined"></i>
  vue
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/前端/" class="nav-link"><i class="iconfont undefined"></i>
  前端
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/后端/" class="nav-link"><i class="iconfont undefined"></i>
  后端
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/node/" class="nav-link"><i class="iconfont undefined"></i>
  node
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/IOS/" class="nav-link"><i class="iconfont undefined"></i>
  IOS
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/WebGIS/" class="nav-link"><i class="iconfont undefined"></i>
  WebGIS
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/React/" class="nav-link"><i class="iconfont undefined"></i>
  React
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/面试/" class="nav-link"><i class="iconfont undefined"></i>
  面试
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/Angular/" class="nav-link"><i class="iconfont undefined"></i>
  Angular
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/数据结构与算法/" class="nav-link"><i class="iconfont undefined"></i>
  数据结构与算法
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/微前端架构/" class="nav-link"><i class="iconfont undefined"></i>
  微前端架构
</a></li></ul></div></div><div class="nav-item"><a href="/blog/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  标签
</a></div><div class="nav-item"><a href="/blog/timeline/" class="nav-link"><i class="iconfont reco-date"></i>
  时间轴
</a></div><div class="nav-item"><a href="/blog/about/" class="nav-link"><i class="iconfont reco-account"></i>
  关于
</a></div> <!----></nav></div></header> <div class="sidebar-mask" data-v-0c5314fc></div> <aside class="sidebar" data-v-0c5314fc><div class="personal-info-wrapper" data-v-03833281 data-v-0c5314fc><img src="/blog/logo.png" alt="author-avatar" class="personal-img" data-v-03833281> <h3 class="name" data-v-03833281>
    茂茂
  </h3> <div class="num" data-v-03833281><div data-v-03833281><h3 data-v-03833281>43</h3> <h6 data-v-03833281>文章</h6></div> <div data-v-03833281><h3 data-v-03833281>30</h3> <h6 data-v-03833281>标签</h6></div></div> <hr data-v-03833281></div> <nav class="nav-links"><div class="nav-item"><a href="/blog/" class="nav-link"><i class="iconfont reco-home"></i>
  主页
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      博客
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/blog/categories/工具与阅读/" class="nav-link"><i class="iconfont undefined"></i>
  工具与阅读
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/vue/" class="nav-link"><i class="iconfont undefined"></i>
  vue
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/前端/" class="nav-link"><i class="iconfont undefined"></i>
  前端
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/后端/" class="nav-link"><i class="iconfont undefined"></i>
  后端
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/node/" class="nav-link"><i class="iconfont undefined"></i>
  node
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/IOS/" class="nav-link"><i class="iconfont undefined"></i>
  IOS
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/WebGIS/" class="nav-link"><i class="iconfont undefined"></i>
  WebGIS
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/React/" class="nav-link"><i class="iconfont undefined"></i>
  React
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/面试/" class="nav-link"><i class="iconfont undefined"></i>
  面试
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/Angular/" class="nav-link"><i class="iconfont undefined"></i>
  Angular
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/数据结构与算法/" class="nav-link"><i class="iconfont undefined"></i>
  数据结构与算法
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/微前端架构/" class="nav-link"><i class="iconfont undefined"></i>
  微前端架构
</a></li></ul></div></div><div class="nav-item"><a href="/blog/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  标签
</a></div><div class="nav-item"><a href="/blog/timeline/" class="nav-link"><i class="iconfont reco-date"></i>
  时间轴
</a></div><div class="nav-item"><a href="/blog/about/" class="nav-link"><i class="iconfont reco-account"></i>
  关于
</a></div> <!----></nav> <!----> </aside> <div class="password-shadow password-wrapper-in" style="display:none;" data-v-5cb9a64e data-v-0c5314fc><h3 class="title" style="display:none;" data-v-5cb9a64e data-v-5cb9a64e>angular知识点梳理第三篇-组件</h3> <!----> <label id="box" class="inputBox" style="display:none;" data-v-5cb9a64e data-v-5cb9a64e><input type="password" value="" data-v-5cb9a64e> <span data-v-5cb9a64e>Konck! Knock!</span> <button data-v-5cb9a64e>OK</button></label> <div class="footer" style="display:none;" data-v-5cb9a64e data-v-5cb9a64e><span data-v-5cb9a64e><i class="iconfont reco-theme" data-v-5cb9a64e></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-5cb9a64e>vuePress-theme-reco</a></span> <span data-v-5cb9a64e><i class="iconfont reco-copyright" data-v-5cb9a64e></i> <a data-v-5cb9a64e><span data-v-5cb9a64e>茂茂</span>
            
          <span data-v-5cb9a64e>2019 - </span>
          2023
        </a></span></div></div></div> <div data-v-0c5314fc><main class="page" style="padding-right:0;"><div class="page-title" style="display:none;"><h1 class="title">angular知识点梳理第三篇-组件</h1> <div class="page-info" data-v-0efa1f05><i class="iconfont reco-account" data-v-0efa1f05><span data-v-0efa1f05>茂茂</span></i> <i class="iconfont reco-date" data-v-0efa1f05><span data-v-0efa1f05>2022-03-15</span></i> <i class="iconfont reco-eye" data-v-0efa1f05><span id="/blog/blogs/tec/2022/031501.html" data-flag-title="Your Article Title" class="leancloud-visitors" data-v-0efa1f05><a class="leancloud-visitors-count" style="font-size:.9rem;font-weight:normal;color:#999;"></a></span></i> <i class="iconfont reco-tag tags" data-v-0efa1f05><span class="tag-item" data-v-0efa1f05>angular</span></i></div></div> <div class="theme-reco-content content__default" style="display:none;"><h2 id="组件介绍"><a href="#组件介绍" class="header-anchor">#</a> 组件介绍</h2> <h3 id="什么是组件"><a href="#什么是组件" class="header-anchor">#</a> 什么是组件</h3> <blockquote><p>组件是 Angular 应用的主要构造块，这个是比较官方的解释，说白话就是完成一定功能的结构块，熟悉的vue框架的可能比较容易理解这句话，vue组件其实就是一个一个的vue文件，组件之间可以进行复用，可以进行数据的传递，不同组件共同构成了一个比较完整的结构化的项目，和vue的区别在于，因为angular的组件是分文件进行的，简单点说就是他分为：</p> <ul><li>一个 HTML 模板，用于声明页面要渲染的内容</li> <li>一个用于定义行为的 Typescript 类</li> <li>一个 CSS 选择器，用于定义组件在模板中的使用方式</li></ul></blockquote> <blockquote><p>vue因为是模版化比较严重的框架，所以vue本身一个vue文件就可以写完一个组件，这点是需要注意的！</p></blockquote> <h3 id="创建一个组件"><a href="#创建一个组件" class="header-anchor">#</a> 创建一个组件</h3> <blockquote><p>方便起见，我这里开始就使用vscode自带的终端进行项目的启动和组件的操作</p></blockquote> <ul><li>命令行</li></ul> <div class="language-js extra-class"><pre class="language-js"><code>ng g component components<span class="token operator">/</span>home
</code></pre></div><blockquote><p>ps: 这里的指令是带有目录的，也就是说我们完全可以直接ng g home 也是可以直接创建一个组件的，但是他会直接出现在app的文件夹下面，这会导致我们在管理组件的时候不太容易管理，所以包括后面我创建组件的时候我也会默认创建到components这个文件夹下面。</p></blockquote> <img src="/angular_create.png"> <ul><li>创建成功</li></ul> <img src="/angular_created.png"> <h3 id="更改默认启动页内容"><a href="#更改默认启动页内容" class="header-anchor">#</a> 更改默认启动页内容</h3> <ul><li>更改app.component.html文件内容</li></ul> <img src="/angular_apphome.png"> <ul><li>查看组件名字</li></ul> <img src="/angular_homename.png"> <ul><li>组件模块介绍</li></ul> <img src="/angular_home.png"> <h3 id="组件的生命周期"><a href="#组件的生命周期" class="header-anchor">#</a> 组件的生命周期</h3> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">import</span> <span class="token punctuation">{</span> Component<span class="token punctuation">,</span> OnInit <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'@angular/core'</span><span class="token punctuation">;</span>

@<span class="token function">Component</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
  selector<span class="token operator">:</span> <span class="token string">'app-home'</span><span class="token punctuation">,</span>
  templateUrl<span class="token operator">:</span> <span class="token string">'./home.component.html'</span><span class="token punctuation">,</span>
  styleUrls<span class="token operator">:</span> <span class="token punctuation">[</span><span class="token string">'./home.component.less'</span><span class="token punctuation">]</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token keyword">export</span> <span class="token keyword">class</span> <span class="token class-name">HomeComponent</span> <span class="token keyword">implements</span> <span class="token class-name">OnInit</span> <span class="token punctuation">{</span>
  <span class="token function">constructor</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token punctuation">}</span>
  <span class="token function">ngOnInit</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">:</span> <span class="token keyword">void</span> <span class="token punctuation">{</span>
    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;ngOnInit====&gt;&quot;</span><span class="token punctuation">)</span>
  <span class="token punctuation">}</span>
  <span class="token function">ngOnChanges</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;ngonchanges====&gt;&quot;</span><span class="token punctuation">)</span>
  <span class="token punctuation">}</span>
  <span class="token function">ngDoCheck</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;ngdocheck====&gt;&quot;</span><span class="token punctuation">)</span>
  <span class="token punctuation">}</span>
  <span class="token function">ngAfterContentInit</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;ngaftercontentinit====&gt;&quot;</span><span class="token punctuation">)</span>
  <span class="token punctuation">}</span>
  <span class="token function">ngAfterContentChecked</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;ngaftercontentchecked====&gt;&quot;</span><span class="token punctuation">)</span>
  <span class="token punctuation">}</span>
  <span class="token function">ngAfterViewInit</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
   console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;ngafterviewinit====&gt;&quot;</span><span class="token punctuation">)</span>
  <span class="token punctuation">}</span>
  <span class="token function">ngAfterViewChecked</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
   console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;ngafterviewchecked====&gt;&quot;</span><span class="token punctuation">)</span>
  <span class="token punctuation">}</span>
  <span class="token function">ngOnDestroy</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
   console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;ngondestory====&gt;&quot;</span><span class="token punctuation">)</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

</code></pre></div><p><a href="https://angular.cn/guide/lifecycle-hooks" target="_blank" rel="noopener noreferrer">生命周期<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></p> <h3 id="组件之间传值"><a href="#组件之间传值" class="header-anchor">#</a> 组件之间传值</h3> <blockquote><p>组件之间传值就是两个组件之间进行数据的交互，组件之间的关系比较多，比如父子组件之间传值，兄弟组件之间传值，下面我们就不同情况进行一个简单的梳理</p></blockquote> <h4 id="父子之间传值"><a href="#父子之间传值" class="header-anchor">#</a> 父子之间传值</h4> <blockquote><p>先搞明白什么算是父子组件，我们创建两个组件，分别是父组件和子组件，两个组件一个被另一个引入，被引入的一个就是子组件，引入的是父组件！父子组件之间传值只是相对的，不是绝对的！比如下面的例子中，根组件引入了parent组件，那么根组件就是父组件，parent组件就是子组件，parent组件引入了children组件，那么parent就是父组件，children就是子组件，他们是一种相对的关系！关系理清了，下面我们开始演示父子组件之间的传值</p></blockquote> <img src="/angular_parent.png"> <ul><li>当前的结构是app引入了parent、parent引入了children</li></ul> <h4 id="父组件传值-函数-给子组件"><a href="#父组件传值-函数-给子组件" class="header-anchor">#</a> 父组件传值(函数)给子组件</h4> <p>第一步：在parent组件的ts文件中 声明一个变量 【parent.component.ts】</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">import</span> <span class="token punctuation">{</span> Component<span class="token punctuation">,</span> OnInit <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'@angular/core'</span><span class="token punctuation">;</span>

@<span class="token function">Component</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
  selector<span class="token operator">:</span> <span class="token string">'app-parent'</span><span class="token punctuation">,</span>
  templateUrl<span class="token operator">:</span> <span class="token string">'./parent.component.html'</span><span class="token punctuation">,</span>
  styleUrls<span class="token operator">:</span> <span class="token punctuation">[</span><span class="token string">'./parent.component.less'</span><span class="token punctuation">]</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token keyword">export</span> <span class="token keyword">class</span> <span class="token class-name">ParentComponent</span> <span class="token keyword">implements</span> <span class="token class-name">OnInit</span> <span class="token punctuation">{</span>
  <span class="token comment">//声明一个需要传递给子组件的变量</span>
  <span class="token keyword">public</span> msg<span class="token operator">:</span> string <span class="token operator">=</span> <span class="token string">&quot;我是parent组件传递过来的msg&quot;</span>
  <span class="token function">constructor</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token punctuation">}</span>
  <span class="token function">ngOnInit</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">:</span> <span class="token keyword">void</span> <span class="token punctuation">{</span>
  <span class="token punctuation">}</span>
  <span class="token comment">//声明一个需要传递给子组件的函数</span>
  <span class="token function">parentRun</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;this is function of parent&quot;</span><span class="token punctuation">)</span>
  <span class="token punctuation">}</span>

<span class="token punctuation">}</span>


</code></pre></div><p>第二步：在父组件的html视图层文件中引入子组件 【parent.component.html】</p> <div class="language-html extra-class"><pre class="language-html"><code><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>p</span><span class="token punctuation">&gt;</span></span>我是父组件<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>p</span><span class="token punctuation">&gt;</span></span>
<span class="token comment">&lt;!-- 引入子组件 --&gt;</span>
<span class="token comment">&lt;!-- 这里的写法是 [msg] 是我们需要发送的变量，这里的名字可以自己定义，后面&quot;msg&quot;是我们ts文件中定义的变量 --&gt;</span>
<span class="token comment">&lt;!-- 这里需要注意的是，我们传递给子组件函数的时候不可以加()，因为加上以后代表函数的执行 --&gt;</span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>app-children</span> <span class="token attr-name">[msg]</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>msg<span class="token punctuation">&quot;</span></span> <span class="token attr-name">[parentRun]</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>parentRun<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>app-children</span><span class="token punctuation">&gt;</span></span>

</code></pre></div><p>第三步：在子组件中的ts文件中使用@Input进行接收父组件的值 【children.component.ts】</p> <div class="language-ts extra-class"><pre class="language-ts"><code><span class="token comment">//这里我们需要引入angular核心模块中的Input模块进行接收父组件的变量值</span>
<span class="token keyword">import</span> <span class="token punctuation">{</span> Component<span class="token punctuation">,</span> OnInit<span class="token punctuation">,</span>Input <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'@angular/core'</span><span class="token punctuation">;</span>

<span class="token decorator"><span class="token at operator">@</span><span class="token function">Component</span></span><span class="token punctuation">(</span><span class="token punctuation">{</span>
  selector<span class="token operator">:</span> <span class="token string">'app-children'</span><span class="token punctuation">,</span>
  templateUrl<span class="token operator">:</span> <span class="token string">'./children.component.html'</span><span class="token punctuation">,</span>
  styleUrls<span class="token operator">:</span> <span class="token punctuation">[</span><span class="token string">'./children.component.less'</span><span class="token punctuation">]</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token keyword">export</span> <span class="token keyword">class</span> <span class="token class-name">ChildrenComponent</span> <span class="token keyword">implements</span> <span class="token class-name">OnInit</span> <span class="token punctuation">{</span>
  <span class="token comment">//使用@Input装饰器进行接收父组件的变量值</span>
  <span class="token decorator"><span class="token at operator">@</span><span class="token function">Input</span></span><span class="token punctuation">(</span><span class="token punctuation">)</span> msg<span class="token operator">:</span><span class="token builtin">any</span>
  <span class="token comment">//使用@Input装饰器接收父组件的函数</span>
  <span class="token decorator"><span class="token at operator">@</span><span class="token function">Input</span></span><span class="token punctuation">(</span><span class="token punctuation">)</span> parentRun<span class="token operator">:</span><span class="token builtin">any</span>
  <span class="token function">constructor</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token punctuation">}</span>

  <span class="token function">ngOnInit</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">:</span> <span class="token keyword">void</span> <span class="token punctuation">{</span>
  <span class="token punctuation">}</span>
  <span class="token comment">/**
   * @function run 子组件本地函数
   */</span>
  <span class="token function">run</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token comment">//通过this进行执行父组件的函数</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">parentRun</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
  <span class="token punctuation">}</span>

<span class="token punctuation">}</span>

</code></pre></div><p>第四步：在子组件的视图层文件中进行变量值的获取 【children.component.html】</p> <div class="language-html extra-class"><pre class="language-html"><code><span class="token comment">&lt;!-- 直接使用{{}}进行变量值的获取，这里的写法和vue的基本是一致的 --&gt;</span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>p</span><span class="token punctuation">&gt;</span></span>{{msg}}<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>p</span><span class="token punctuation">&gt;</span></span>
<span class="token comment">&lt;!-- 这里和vue的区别在于，vue调用函数是需要@click，angular需要的是(click) 只是语法上的区别，执行过程是一致的 --&gt;</span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>button</span> <span class="token attr-name">(click)</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>run()<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
    执行parentRun
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>button</span><span class="token punctuation">&gt;</span></span>

</code></pre></div><h4 id="传递整个父组件"><a href="#传递整个父组件" class="header-anchor">#</a> 传递整个父组件</h4> <blockquote><p>解释一下，这里可能业务场景不太多，因为我也没实战过angular的项目，不太清楚这块是不是真的会有需要，不过这里还是要讲一下，整个父组件传递给子组件写法如下：</p></blockquote> <p>在父组件的视图层文件中实现this的传递 【parent.component.html】</p> <div class="language-html extra-class"><pre class="language-html"><code><span class="token comment">&lt;!-- 这里的this 指的就是当前组件 home是自定义的 可以随意命名--&gt;</span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>app-children</span> <span class="token attr-name">[home]</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>this<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>app-children</span><span class="token punctuation">&gt;</span></span>

</code></pre></div><p>在子组件中进行@Input进行接收即可 【children.component.html】</p> <div class="language-ts extra-class"><pre class="language-ts"><code><span class="token comment">//这里我们需要引入angular核心模块中的Input模块进行接收父组件的变量值</span>
<span class="token keyword">import</span> <span class="token punctuation">{</span> Component<span class="token punctuation">,</span> OnInit<span class="token punctuation">,</span>Input <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'@angular/core'</span><span class="token punctuation">;</span>

<span class="token decorator"><span class="token at operator">@</span><span class="token function">Component</span></span><span class="token punctuation">(</span><span class="token punctuation">{</span>
  selector<span class="token operator">:</span> <span class="token string">'app-children'</span><span class="token punctuation">,</span>
  templateUrl<span class="token operator">:</span> <span class="token string">'./children.component.html'</span><span class="token punctuation">,</span>
  styleUrls<span class="token operator">:</span> <span class="token punctuation">[</span><span class="token string">'./children.component.less'</span><span class="token punctuation">]</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token keyword">export</span> <span class="token keyword">class</span> <span class="token class-name">ChildrenComponent</span> <span class="token keyword">implements</span> <span class="token class-name">OnInit</span> <span class="token punctuation">{</span>
  <span class="token comment">//使用@Input装饰器接收整个父组件</span>
  <span class="token decorator"><span class="token at operator">@</span><span class="token function">Input</span></span><span class="token punctuation">(</span><span class="token punctuation">)</span> home<span class="token operator">:</span><span class="token builtin">any</span>
  <span class="token function">constructor</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token punctuation">}</span>

  <span class="token function">ngOnInit</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">:</span> <span class="token keyword">void</span> <span class="token punctuation">{</span>
  <span class="token punctuation">}</span>
  <span class="token comment">/**
   * @function run 子组件本地函数
   */</span>
  <span class="token function">run</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token builtin">console</span><span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>home<span class="token punctuation">.</span>msg<span class="token punctuation">)</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>home<span class="token punctuation">.</span><span class="token function">parentRun</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>



</code></pre></div><blockquote><p>以上就是父组件向子组件传值的方式，如果还有不明白的可以多看几遍！如果是第一次接触angular这样的框架，可能会有一些不太适应它的写法，不过没太大问题，习惯了她的写法就比较容易理解了！当然上面是传递的数据，其实方法也是可以进行传递的，上面的demo，子组件中执行父组件的方法也有对应的代码实现！</p></blockquote> <h3 id="子组件传值-函数-给父组件"><a href="#子组件传值-函数-给父组件" class="header-anchor">#</a> 子组件传值(函数)给父组件</h3> <p>方案一 通过viewchild进行节点获取
第一步：在父组件引入子组件的地方添加节点值 【parent.component.html】</p> <div class="language-html extra-class"><pre class="language-html"><code><span class="token comment">&lt;!-- 这里给子组件起一个节点名字，为后续父组件获取该节点的值提供名字 --&gt;</span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>app-children</span> <span class="token attr-name">#childrenNode</span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>app-children</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>button</span> <span class="token attr-name">(click)</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>passfun()<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span> 执行子组件的childfunc方法<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>button</span><span class="token punctuation">&gt;</span></span>

</code></pre></div><p>第二步：在子组件中声明一些需要传递的变量 【children.component.ts】</p> <div class="language-ts extra-class"><pre class="language-ts"><code><span class="token comment">//这里我们需要引入angular核心模块中的Input模块进行接收父组件的变量值</span>
<span class="token keyword">import</span> <span class="token punctuation">{</span> Component<span class="token punctuation">,</span> OnInit<span class="token punctuation">,</span>Input <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'@angular/core'</span><span class="token punctuation">;</span>

<span class="token decorator"><span class="token at operator">@</span><span class="token function">Component</span></span><span class="token punctuation">(</span><span class="token punctuation">{</span>
  selector<span class="token operator">:</span> <span class="token string">'app-children'</span><span class="token punctuation">,</span>
  templateUrl<span class="token operator">:</span> <span class="token string">'./children.component.html'</span><span class="token punctuation">,</span>
  styleUrls<span class="token operator">:</span> <span class="token punctuation">[</span><span class="token string">'./children.component.less'</span><span class="token punctuation">]</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token keyword">export</span> <span class="token keyword">class</span> <span class="token class-name">ChildrenComponent</span> <span class="token keyword">implements</span> <span class="token class-name">OnInit</span> <span class="token punctuation">{</span>
 
  <span class="token function">constructor</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token punctuation">}</span>
  <span class="token keyword">public</span> childmsg<span class="token operator">:</span><span class="token builtin">any</span> <span class="token operator">=</span> <span class="token string">&quot;我是子组件中即将被父组件进行获取的变量&quot;</span>

  <span class="token function">ngOnInit</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">:</span> <span class="token keyword">void</span> <span class="token punctuation">{</span>
  <span class="token punctuation">}</span>
  <span class="token function">childfunc</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token builtin">console</span><span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;我是子组件的函数&quot;</span><span class="token punctuation">)</span>
  <span class="token punctuation">}</span>
  <span class="token comment">/**
   * @function run 子组件本地函数
   */</span>
  <span class="token function">run</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>home<span class="token punctuation">.</span><span class="token function">parentRun</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token builtin">console</span><span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;this is run&quot;</span><span class="token punctuation">)</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>


</code></pre></div><p>第三步：在父组件的ts文件中引入viewchild模块 【parent.component.ts】</p> <div class="language-ts extra-class"><pre class="language-ts"><code><span class="token comment">// 引入angular核心模块的viewchild模块</span>
<span class="token keyword">import</span> <span class="token punctuation">{</span> Component<span class="token punctuation">,</span> OnInit<span class="token punctuation">,</span>ViewChild <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'@angular/core'</span><span class="token punctuation">;</span>

<span class="token decorator"><span class="token at operator">@</span><span class="token function">Component</span></span><span class="token punctuation">(</span><span class="token punctuation">{</span>
  selector<span class="token operator">:</span> <span class="token string">'app-parent'</span><span class="token punctuation">,</span>
  templateUrl<span class="token operator">:</span> <span class="token string">'./parent.component.html'</span><span class="token punctuation">,</span>
  styleUrls<span class="token operator">:</span> <span class="token punctuation">[</span><span class="token string">'./parent.component.less'</span><span class="token punctuation">]</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token keyword">export</span> <span class="token keyword">class</span> <span class="token class-name">ParentComponent</span> <span class="token keyword">implements</span> <span class="token class-name">OnInit</span> <span class="token punctuation">{</span>
<span class="token comment">//使用viewchild装饰器进行节点值的获取</span>
  <span class="token decorator"><span class="token at operator">@</span><span class="token function">ViewChild</span></span><span class="token punctuation">(</span><span class="token string">'childrenNode'</span><span class="token punctuation">)</span> children<span class="token operator">:</span><span class="token builtin">any</span>
  <span class="token function">constructor</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token punctuation">}</span>
  <span class="token function">ngOnInit</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">:</span> <span class="token keyword">void</span> <span class="token punctuation">{</span>
  <span class="token punctuation">}</span>
  <span class="token comment">//声明一个需要传递给子组件的函数</span>
  <span class="token function">parentRun</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
  <span class="token comment">//通过viewchild进行子组件数据的获取</span>
    <span class="token builtin">console</span><span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>children<span class="token punctuation">)</span>
    <span class="token builtin">console</span><span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>children<span class="token punctuation">.</span>childmsg<span class="token punctuation">)</span>
  <span class="token punctuation">}</span>
  <span class="token comment">//父组件执行子组件的函数</span>
  <span class="token function">passfun</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>children<span class="token punctuation">.</span><span class="token function">childfunc</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>


</code></pre></div><p>方案二：通过@Output触发父组件的方法</p> <blockquote><p>这个方式就是同归广播的方式进行触发函数，将子组件中的数据主动传递到父组件中去</p></blockquote> <p>第一步：在子组件ts文件中引入angular的核心模块中的output和EventEmitter模块 【children.component.ts】</p> <div class="language-ts extra-class"><pre class="language-ts"><code><span class="token comment">//这里我们需要引入angular核心模块中的Input模块进行接收父组件的变量值</span>
<span class="token keyword">import</span> <span class="token punctuation">{</span> Component<span class="token punctuation">,</span> OnInit<span class="token punctuation">,</span>Input<span class="token punctuation">,</span>Output<span class="token punctuation">,</span>EventEmitter <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'@angular/core'</span><span class="token punctuation">;</span>

<span class="token decorator"><span class="token at operator">@</span><span class="token function">Component</span></span><span class="token punctuation">(</span><span class="token punctuation">{</span>
  selector<span class="token operator">:</span> <span class="token string">'app-children'</span><span class="token punctuation">,</span>
  templateUrl<span class="token operator">:</span> <span class="token string">'./children.component.html'</span><span class="token punctuation">,</span>
  styleUrls<span class="token operator">:</span> <span class="token punctuation">[</span><span class="token string">'./children.component.less'</span><span class="token punctuation">]</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token keyword">export</span> <span class="token keyword">class</span> <span class="token class-name">ChildrenComponent</span> <span class="token keyword">implements</span> <span class="token class-name">OnInit</span> <span class="token punctuation">{</span>
  <span class="token function">constructor</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token punctuation">}</span>
  <span class="token comment">//通过Output进行子组件给父组件传递数据 childOut 是自己随意起的名字</span>
  <span class="token decorator"><span class="token at operator">@</span><span class="token function">Output</span></span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">public</span> childOut <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">EventEmitter</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
  <span class="token function">ngOnInit</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">:</span> <span class="token keyword">void</span> <span class="token punctuation">{</span>
  <span class="token punctuation">}</span>
  <span class="token comment">//声明一个使用output方式传递数据的函数</span>
  <span class="token function">passOutput</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>childOut<span class="token punctuation">.</span><span class="token function">emit</span><span class="token punctuation">(</span><span class="token string">&quot;我是子组件的output方法&quot;</span><span class="token punctuation">)</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

</code></pre></div><p>第二步：在父组件的视图层引入子组件的地方进行函数名字的使用 【parent.component.html】</p> <div class="language-html extra-class"><pre class="language-html"><code><span class="token comment">&lt;!-- (childOut) 就是子组件中自己起的名字  $event可以写也可以不写，这里就是子组件传递的数据--&gt;</span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>app-children</span> <span class="token attr-name">(childOut)</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>parentInput($event)<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>app-children</span><span class="token punctuation">&gt;</span></span>

</code></pre></div><p>第三步：在父组件的ts文件中进行函数和数据的执行 【parent.component.ts】</p> <div class="language-ts extra-class"><pre class="language-ts"><code><span class="token keyword">import</span> <span class="token punctuation">{</span> Component<span class="token punctuation">,</span> OnInit<span class="token punctuation">,</span>ViewChild <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'@angular/core'</span><span class="token punctuation">;</span>

<span class="token decorator"><span class="token at operator">@</span><span class="token function">Component</span></span><span class="token punctuation">(</span><span class="token punctuation">{</span>
  selector<span class="token operator">:</span> <span class="token string">'app-parent'</span><span class="token punctuation">,</span>
  templateUrl<span class="token operator">:</span> <span class="token string">'./parent.component.html'</span><span class="token punctuation">,</span>
  styleUrls<span class="token operator">:</span> <span class="token punctuation">[</span><span class="token string">'./parent.component.less'</span><span class="token punctuation">]</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token keyword">export</span> <span class="token keyword">class</span> <span class="token class-name">ParentComponent</span> <span class="token keyword">implements</span> <span class="token class-name">OnInit</span> <span class="token punctuation">{</span>
  <span class="token function">constructor</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token punctuation">}</span>
  <span class="token function">ngOnInit</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">:</span> <span class="token keyword">void</span> <span class="token punctuation">{</span>
  <span class="token punctuation">}</span>
  <span class="token comment">//通过output进行子组件数据的获取</span>
  <span class="token function">parentInput</span><span class="token punctuation">(</span>e<span class="token operator">:</span><span class="token builtin">any</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token builtin">console</span><span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;执行了....&quot;</span><span class="token punctuation">)</span>
    <span class="token builtin">console</span><span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>e<span class="token punctuation">)</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>


</code></pre></div></div> <footer class="page-edit" style="display:none;"><!----> <div class="last-updated"><span class="prefix">Last Updated: </span> <span class="time">2023/3/10 下午3:31:11</span></div></footer> <!----> <!----> <!----></main> <!----></div></div></div></div></div><div class="global-ui"><div class="back-to-ceiling" style="right:1rem;bottom:6rem;width:2.5rem;height:2.5rem;border-radius:.25rem;line-height:2.5rem;display:none;" data-v-c6073ba8 data-v-c6073ba8><svg t="1574745035067" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5404" class="icon" data-v-c6073ba8><path d="M526.60727968 10.90185116a27.675 27.675 0 0 0-29.21455937 0c-131.36607665 82.28402758-218.69155461 228.01873535-218.69155402 394.07834331a462.20625001 462.20625001 0 0 0 5.36959153 69.94390903c1.00431239 6.55289093-0.34802892 13.13561351-3.76865779 18.80351572-32.63518765 54.11355614-51.75690182 118.55860487-51.7569018 187.94566865a371.06718723 371.06718723 0 0 0 11.50484808 91.98906777c6.53300375 25.50556257 41.68394495 28.14064038 52.69160883 4.22606766 17.37162448-37.73630017 42.14135425-72.50938081 72.80769204-103.21549295 2.18761121 3.04276886 4.15646224 6.24463696 6.40373557 9.22774369a1871.4375 1871.4375 0 0 0 140.04691725 5.34970492 1866.36093723 1866.36093723 0 0 0 140.04691723-5.34970492c2.24727335-2.98310674 4.21612437-6.18497483 6.3937923-9.2178004 30.66633723 30.70611158 55.4360664 65.4791928 72.80769147 103.21549355 11.00766384 23.91457269 46.15860503 21.27949489 52.69160879-4.22606768a371.15156223 371.15156223 0 0 0 11.514792-91.99901164c0-69.36717486-19.13165746-133.82216804-51.75690182-187.92578088-3.42062944-5.66790279-4.76302748-12.26056868-3.76865837-18.80351632a462.20625001 462.20625001 0 0 0 5.36959269-69.943909c-0.00994388-166.08943902-87.32547796-311.81420293-218.6915546-394.09823051zM605.93803103 357.87693858a93.93749974 93.93749974 0 1 1-187.89594924 6.1e-7 93.93749974 93.93749974 0 0 1 187.89594924-6.1e-7z" p-id="5405" data-v-c6073ba8></path><path d="M429.50777625 765.63860547C429.50777625 803.39355007 466.44236686 1000.39046097 512.00932183 1000.39046097c45.56695499 0 82.4922232-197.00623328 82.5015456-234.7518555 0-37.75494459-36.9345906-68.35043303-82.4922232-68.34111062-45.57627738-0.00932239-82.52019037 30.59548842-82.51086798 68.34111062z" p-id="5406" data-v-c6073ba8></path></svg></div><div class="kanbanniang" data-v-5775ee02><div class="banniang-container" style="display:;" data-v-5775ee02><div class="messageBox" style="right:68px;bottom:190px;display:none;" data-v-5775ee02>
      我是王茂铭谢谢你的关注 
    </div> <div class="operation" style="right:90px;bottom:40px;display:;" data-v-5775ee02><i class="kbnfont kbn-ban-home ban-home" data-v-5775ee02></i> <i class="kbnfont kbn-ban-message message" data-v-5775ee02></i> <i class="kbnfont kbn-ban-close close" data-v-5775ee02></i> <a target="_blank" href="https://vuepress-theme-reco.recoluan.com/views/plugins/kanbanniang.html" data-v-5775ee02><i class="kbnfont kbn-ban-info info" data-v-5775ee02></i></a> <i class="kbnfont kbn-ban-theme skin" style="display:;" data-v-5775ee02></i></div> <canvas id="banniang" width="240" height="352" class="live2d" style="right:90px;bottom:-20px;opacity:0.9;" data-v-5775ee02></canvas></div> <div class="showBanNiang" style="display:none;" data-v-5775ee02>
    看板娘
  </div></div><canvas id="vuepress-canvas-cursor"></canvas><APlayer audio="" fixed="true" mini="true" autoplay="autoplay" theme="#b7daff" loop="loop" order="random" preload="auto" volume="0.2" mutex="true" lrc-type="1" list-max-height="250" storage-name="vuepress-plugin-meting" id="aplayer-fixed"></APlayer></div></div>
    <script src="/blog/assets/js/app.e4cba77b.js" defer></script><script src="/blog/assets/js/5.1ad0ff24.js" defer></script><script src="/blog/assets/js/1.fe4eac58.js" defer></script><script src="/blog/assets/js/47.499ce120.js" defer></script>
  </body>
</html>
